package com.cqgcxy.shopping.controller;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cqgcxy.shopping.common.CommException;
import com.cqgcxy.shopping.domain.User;
import com.cqgcxy.shopping.service.UserService;

@WebServlet(urlPatterns="/info.do")


public class InformationServlet extends HttpServlet{

	private static final long serialVersionUID = -6204376603444672194L;
	private Logger  logger = LoggerFactory.getLogger(getClass());
	private UserService userService=new UserService();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		logger.info("1111");
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		logger.info("接受到information.do.dopost");
		try {
			// 1.抽取参数，封装为实体对象
			String username=req.getParameter("username");  //用户自定义的名称
			username = username.replaceAll(" ", ""); //去掉用户自定义的名称中的空格
			String userzhenshiname=req.getParameter("userzhenshiname");//用户的真实姓名
			userzhenshiname = userzhenshiname.replaceAll(" ", "");
			String user_sex=req.getParameter("user_sex");   //用户的性别
			String user_nian=req.getParameter("user_nian");//用户生日的年份
			String user_yue=req.getParameter("user_yue");//用户生日的月份
			String user_day=req.getParameter("user_day");//用户生日的日子
			String email=req.getParameter("email");//用户邮箱
			String phonenumber=req.getParameter("phonenumber");//用户修改的电话
			String usercode=req.getParameter("usercode");  //用户自定义的名称
			usercode = usercode.replaceAll(" ", ""); //去掉用户自定义的名称中的空格
			logger.debug("验证 1  获取参数成功：用户名：{},用户真实姓名：{},用户性别：{},用户生日：{}年,{}月,{}日,用户的邮箱：{},用户修改的电话：{},用户账号：{}",username,userzhenshiname,user_sex,user_nian,user_yue,user_day,email,phonenumber,usercode);			

			
			
			// 2.类型转换
			logger.debug("类型转换成功");
			// 3.数据校验
			//账号不为空
			/*if(usercode==null||usercode.trim().length()<=0) {
				throw new CommException("账号不能为空");
			}
			//密码不能为空
			if(password==null||password.trim().length()<6) {
				throw new CommException("密码长度错误");
			}*/
		/*	if(kaptcha==null||kaptcha.trim().length()<0) {
				throw new CommException("验证码错误");
			}
			
			Object object=session.getAttribute("kaptcha"); //获取session中的验证码
			if(!kaptcha.equals(object)) {
				throw new CommException("验证码输入错误");
			}*/
			logger.debug("校验成功");
			//4.参数封装为对象
			User user = new User();

			user.setUsername(username);
			user.setUserzhenshiname(userzhenshiname);
			user.setUser_sex(user_sex);
			user.setUser_nian(user_nian);
			user.setUser_yue(user_yue);
			user.setUser_day(user_day);
			user.setEmail(email);
			user.setPhonenumber(phonenumber);

			
			// 5.调用业务方法
			
			HttpSession session=req.getSession();
			session.setAttribute("user", user);
			logger.debug("业务处理成功");
			userService.xiugai(username,userzhenshiname,user_sex,user_nian,user_yue,user_day,email,phonenumber,usercode);
			// 6.转发或者重定向

			resp.sendRedirect(req.getContextPath()+"/index.jsp");//重定向
			logger.debug("修改个人信息成功");
			
		} catch (Exception e) {
			/*req.setAttribute("error", e.getMessage());
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			logger.info("登录失败:{}",e.getMessage());*/

			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			logger.info("修改失败:{}",e.getMessage());
		}
		
	}
}
